#include "p2p.h"


#ifdef  HAVE_SOCKADDR_DL_STRUCT
#include <net/if_dl.h>
#endif

char *sock_ntop_host(const struct sockaddr *sa, socklen_t salen)
{
	static char str[128];   //unix domain is largest

	switch (sa->sa_family)
	{
		case AF_INET:
		{
			 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
			if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL)					return(NULL);
			return(str);
		}
		
		#ifdef IPV6
		case AF_INET6:
		{
			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
			if (inet_ntop(AF_INET6, &sin6->sin6_addr, str, sizeof(str)) ==NULL)
				return(NULL);
			return(str);

		}
		#endif

		#ifdef AF_UNIX
		case AF_UNIX:
		{
			struct sockaddr_un *unp = (struct sockaddr_un *)sa;
			if (unp->sun_path[0] == 0)
				strcpy(str,"no pathname bound");
			else
				snprintf(str, sizeof(str), "%s", unp->sun_path);
			return(str);

		}
		#endif
		default:
			snprintf(str, sizeof(str), "sock_ntop_host:unknow AF_XXX:%d, len %d", sa->sa_family, salen);
			return(str);

	}

	return (NULL);

}




char *Sock_ntop_host(const struct sockaddr *sa, socklen_t salen)
{
	char *ptr;

	if ( (ptr = sock_ntop_host(sa, salen)) == NULL)
		perror("sock_ntop_host error");
	return (ptr);
}

